import socket
import threading
import time

def recv_msg(sock):
    while True:
        data = sock.recv(2048)
        if len(data) == 0:
            break
        data = data.decode()
        if data in ("q", "exit"):
            break
        print("recv: ", data)
    print("recv thread exit")


def send_msg(sock):
    while True:
        msg = input("请输入发送msg:")
        sock.send(msg.encode())
        time.sleep(0.5)
        if msg in ("q", "exit"):
            break
    print("send thread exit")


if __name__ == "__main__":
    sock1, sock2 = socket.socketpair()
    '''
    lsof -p pid
    python3 25053 liuj    3u  IPv4             294654      0t0     TCP localhost:41076->localhost:44579 (ESTABLISHED)
    socketpair 生成本地 localhost 已连接的socket
    '''
    task_send = threading.Thread(target=send_msg, args=(sock1,))
    task_send.start()
    recv_msg(sock2)
    task_send.join()

    sock1.close()
    sock2.close()
    print("main thread exit")
